package com.huajx.tuopuda.client.udp;


import com.huajx.tuopuda.client.udp.handler.ReadRunStatusHandler;
import com.huajx.util.ByteUtil;
import com.huajx.util.HexUtil;

import java.util.Arrays;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 解码器
 *
 * @description:
 * @author: lisa 叶新华
 * @create: 2019-09-24 13:34
 */
public class TuopudaDecode {
	private static final Logger logger = LoggerFactory.getLogger(TuopudaDecode.class);

    /**
     * 解码处理
     *
     * @param bytes 需解码的字节数组
     * @return
     * @throws TuopudaException
     */
    public TuopudaMessage decode(byte[] bytes) throws TuopudaException {
        if (bytes.length != 34) { //错误计数
        	if(ReadRunStatusHandler.errorNum<=9000){
    			ReadRunStatusHandler.errorNum++;//错误计数
    			ReadRunStatusHandler.successNum=0;
    			if(ReadRunStatusHandler.errorNum==10){
    				ReadRunStatusHandler.onOff=false;
    			}
        		if(ReadRunStatusHandler.errorNum>=9000){
        			ReadRunStatusHandler.errorNum=0;
        		}
        		System.out.println("错误数："+ReadRunStatusHandler.errorNum);
        	}
        	
            throw new TuopudaException("长度不够: " + HexUtil.bytesToHex(bytes));
        }
        TuopudaMessage message = new TuopudaMessage();
        int direct = ByteUtil.bytesToShort(new byte[]{bytes[3], bytes[4]});

        message.setDirect(direct);

        message.setBytes(bytes);

        switch (direct) {
            case TuopudaDirect.D_1081:
                readRunStatus(message, bytes);
                break;
            case TuopudaDirect.D_108e:
            	 readRunStatus(message, bytes);
                break;
            case TuopudaDirect.D_108d:
            	readReadRecord(message, bytes);
                break;
            case TuopudaDirect.D_108B:
            	 setTime(message,bytes);
            	 break;
            default:
                throw new TuopudaException("该命令没有实现解码处理"+direct);
        }
        //if(!ReadRunStatusHandler.onOff){
    	if(ReadRunStatusHandler.successNum<=9000){
    		ReadRunStatusHandler.successNum++;//成功计数
    		ReadRunStatusHandler.errorNum=0;//错误计数
    		if(ReadRunStatusHandler.successNum>=4){
    			ReadRunStatusHandler.onOff=true;
    		}
    		if(ReadRunStatusHandler.successNum>=9000){
    			ReadRunStatusHandler.successNum=0;
    		}
    		//System.out.println("正确数："+ReadRunStatusHandler.successNum);
		}
        //}
        return message;
    }
    /**
     * 读取
     */
    public void setTime(TuopudaMessage message, byte[] bytes){
    	 byte[] attendDateBytesYMd = Arrays.copyOfRange(bytes, 5, 8);
    	 byte[] attendDateBytesHms = Arrays.copyOfRange(bytes, 9, 12);
    	 byte[] attendDateBytesWeek = Arrays.copyOfRange(bytes, 8, 9);
    	 /*System.out.println("设置时间");
    	 System.out.println(HexUtil.bytesToHex(attendDateBytes));*/
    	 String ymd = HexUtil.bytesToHex(attendDateBytesYMd);
    	 String hms = HexUtil.bytesToHex(attendDateBytesHms);
    	 String week = HexUtil.bytesToHex(attendDateBytesWeek);
    	 logger.info("已校准时间: 日期为{},时间为{},周{}",ymd,hms,week);
    }
    
    /**
     * 读取开门记录
     * @param message
     * @param bytes
     */
    public void readReadRecord(TuopudaMessage message, byte[] bytes){
    	 byte[] cardIdBytes = Arrays.copyOfRange(bytes, 5, 9);
    	 int cardId = ByteUtil.bytesToInt(cardIdBytes);
    	 byte[] attendDateBytes = Arrays.copyOfRange(bytes, 9, 13);
    	 int address = ByteUtil.bytesToInt(new byte[]{bytes[1], bytes[2]});
    	 Date attendDate = TuopudaUtil.bytesToDate(attendDateBytes);
    	 message.setAddress(address);
         message.setCarId(cardId);
         message.setDate(attendDate);//设置为工控机时间
         message.setInfo(HexUtil.bytesToHex(bytes));
    }
    /**
     * 读取运行状态信息
     * @param message
     * @param bytes
     */
    public void readRunStatus(TuopudaMessage message, byte[] bytes) {
        byte[] recordsBytes = Arrays.copyOfRange(bytes, 12, 15);
        byte[] cardIdBytes = Arrays.copyOfRange(bytes, 17, 21);
        byte[] attendDateBytes = Arrays.copyOfRange(bytes, 21, 25);

        int address = ByteUtil.bytesToInt(new byte[]{bytes[1], bytes[2]});
        int records = ByteUtil.bytesToInt(recordsBytes);
        int cardId = ByteUtil.bytesToInt(cardIdBytes);
        Date attendDate = TuopudaUtil.bytesToDate(attendDateBytes);

        message.setAddress(address);
        message.setCarId(cardId);
        message.setDate(attendDate);
        message.setRecords(records);

        byte recordStatus = cardIdBytes[3]; // 记录状态

        message.setMark(recordStatus);
    }
//    /**
//     * 读取刷卡记录
//     * @param message
//     * @param bytes
//     */
//    public void readReocrd(TuopudaMessage message, byte[] bytes){
//        byte[] cardIdBytes = Arrays.copyOfRange(bytes, 5, 6);//ArrayUtil.sub(bytes, 17, 21);
//        byte[] attendDateBytes = Arrays.copyOfRange(bytes, 9, 12);
//        int address = BitUtil.bytesToInt(new byte[]{bytes[1], bytes[2]});
//        //int = BitUtil.bytesToInt(new byte[]{bytes[8], bytes[8]});
//        int cardId = BitUtil.bytesToInt(cardIdBytes);
//        Date attendDate = TuopudaUtil.bytesToDate(attendDateBytes);
//        message.setAddress(address);
//        message.setCarId(cardId);
//        message.setDate(attendDate);
//
//    }

    /**
     * 板地址
     *
     * @param bytes
     * @return
     */
    public static int address(byte[] bytes) {
        return 0;
    }
}
